import { useState, useCallback, useRef } from 'react';

export const useTooltip = () => {
    const [tooltip, setTooltip] = useState({ 
        show: false, 
        data: null, 
        position: { x: 0, y: 0 },
        pinned: false,
    });
    const timeoutRef = useRef(null);

    const showTooltip = useCallback((item, event) => {
        // 若已固定，则不跟随 hover 更新到其他卡片
        if (tooltip.pinned) return;
        // 清除之前的延时
        if (timeoutRef.current) {
            clearTimeout(timeoutRef.current);
            timeoutRef.current = null;
        }

        const rect = event.currentTarget.getBoundingClientRect();
        const scrollY = window.scrollY;
        const scrollX = window.scrollX;
        const viewportWidth = window.innerWidth;
        const viewportHeight = window.innerHeight;
        const tooltipWidth = 400;
        const tooltipHeight = 300; // 估算高度

        // 计算水平位置
        let left = rect.right + 10 + scrollX;
        if (left + tooltipWidth > viewportWidth + scrollX - 20) {
            left = rect.left - tooltipWidth - 10 + scrollX;
        }
        if (left < scrollX + 10) {
            left = scrollX + 10;
        }

        // 计算垂直位置
        let top = rect.top + scrollY;
        if (top + tooltipHeight > viewportHeight + scrollY - 20) {
            top = Math.max(scrollY + 10, rect.bottom - tooltipHeight + scrollY);
        }

        // 使用最新的item快照，避免闭包中引用到旧项
        setTooltip({
            show: true,
            data: JSON.parse(JSON.stringify(item)),
            position: {
                x: left,
                y: top
            },
            pinned: false,
        });
    }, [tooltip.pinned]);

    const pinTooltip = useCallback((item, event) => {
        if (timeoutRef.current) {
            clearTimeout(timeoutRef.current);
            timeoutRef.current = null;
        }

        const rect = event.currentTarget.getBoundingClientRect();
        const left = rect.right + 10 + window.scrollX;
        const top = rect.top + window.scrollY;

        setTooltip({
            show: true,
            data: JSON.parse(JSON.stringify(item)),
            position: { x: left, y: top },
            pinned: true,
        });
    }, []);

    const hideTooltip = useCallback(() => {
        // 设置延时隐藏，避免鼠标快速移动时闪烁
        if (tooltip.pinned) return; // 固定时不隐藏
        timeoutRef.current = setTimeout(() => {
            setTooltip({ 
                show: false, 
                data: null, 
                position: { x: 0, y: 0 },
                pinned: false,
            });
        }, 150);
    }, [tooltip.pinned]);

    const keepTooltip = useCallback(() => {
        // 鼠标进入tooltip时保持显示
        if (timeoutRef.current) {
            clearTimeout(timeoutRef.current);
            timeoutRef.current = null;
        }
    }, []);

    const unpinTooltip = useCallback(() => {
        setTooltip(prev => ({ ...prev, pinned: false }));
    }, []);

    return {
        tooltip,
        showTooltip,
        hideTooltip,
        keepTooltip,
        pinTooltip,
        unpinTooltip,
    };
};